package com.example.recite_words.Bean;
import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.example.recite_words.R;
import com.example.recite_words.util.RWfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

class Battle_info implements Serializable{
    private String rank = "青铜";
    private int win_num = 0;
    private int battle_num = 0;

    Battle_info(){
        rank = " 青铜";
        win_num = 0;
        battle_num  = 0;
    }
    Battle_info(JSONObject jsonObject){
        rank = (String) jsonObject.get("rank");
        win_num = (int) jsonObject.get("win_num");
        battle_num = (int) jsonObject.get("battle_num");
    }
}
public class User implements Serializable {
    private String FileType = null; //头像文件类型
    private String id =null;    //id 即邮箱
    private String NickName = "未命名";// 用户昵称



    private String password =null;
    private int cookie;
    private ArrayList<Battle_info> battle_infos = new ArrayList<>();
    private ArrayList<Word> wrong = new ArrayList<>();
    private String iconFile = null;
    public User(){

    }
    public User(String id,String nickName){
        this.id = id;
        battle_infos.add(new Battle_info());
        battle_infos.add(new Battle_info());
        battle_infos.add(new Battle_info());
        wrong = new ArrayList<Word>();
        cookie = 0x00000;
    }
    public User(String nickName,String id,String password){
        this.NickName = nickName;
        this.id = id;
        this.password = password;
        battle_infos.add(new Battle_info());
        battle_infos.add(new Battle_info());
        battle_infos.add(new Battle_info());
        wrong = new ArrayList<Word>();
        cookie = 0x11111;
    }
    public User(JSONObject jsonObject){
        id = (String) jsonObject.get("id");
        NickName = (String)jsonObject.get("name");
        cookie = 0x11;
        //battle_infos.add(new Battle_info((JSONObject) jsonObject.get("cet4")));
       // battle_infos.add(new Battle_info((JSONObject) jsonObject.get("cet6")));
       // battle_infos.add(new Battle_info((JSONObject) jsonObject.get("toefl")));
      //  wrong = (ArrayList<Word>) jsonObject.get("wrong");
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNickName() {
        return NickName;
    }

    public void setNickName(String nickName) {
        NickName = nickName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public ArrayList<Battle_info> getBattle_infos() {
        return battle_infos;
    }

    public void setBattle_infos(ArrayList<Battle_info> battle_infos) {
        this.battle_infos = battle_infos;
    }
    public String getFileType() {
        return FileType;
    }

    public void setFileType(String fileType) {
        FileType = fileType;
    }

    public String getIconFile() {
        return iconFile;
    }

    public void setIconFile(String iconFile) {
        this.iconFile = iconFile;
    }

    public ArrayList<Word> getWrong() {
        return wrong;
    }

    public void setWrong(ArrayList<Word> wrong) {
        this.wrong = wrong;
    }

    public String getIcon() {
        return iconFile;
    }

    public void setIcon(String icon) {
        this.iconFile = icon;
    }

    public int getCookie() {
        return cookie;
    }

    public void setCookie(int cookie) {
        this.cookie = cookie;
    }
    public void WriteUser(Context context) throws IOException {
        String path = context.getExternalFilesDir(null).toString();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path + "/User.txt"));
        oos.writeObject(this);
    }
    public User ReadUser(Context context) throws IOException, ClassNotFoundException {
        String path = context.getExternalFilesDir(null).toString();
        File fileJA = new File(path);
        if (!fileJA.exists()) {
            fileJA.mkdirs();
        }
        File file = new File(path + "/User.txt");
        if(file.exists() && file.length() != 0){
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path+"/User.txt"));
        User user = (User) ois.readObject();
        return user;}
        User user = new User();
        user.setCookie(0);
        return user;
    }
    public Uri IconToUri(){
        if (this.getIcon() == null || this.getIcon().equals("")){
            return null;
        }
        File file = new File(this.getIcon().substring(1,this.getIcon().length()));
        return Uri.fromFile(file);
    }
}
